package com.gdpu.project.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.gdpu.page.vo.PageView;
import com.gdpu.page.vo.QueryResult;
import com.gdpu.project.service.ProjectService;
import com.gdpu.project.vo.Project;
import com.opensymphony.xwork2.ActionSupport;

public class ProjectAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private Integer id;
	private String projectId;
	private String projectType;
	private String projectName;
	private String projectOrgin;
	private String projectXz;
	private Date stratDate;
	private Date planEndDate;
	private String cyrs;
	private String comment;
	private String projectFile;
	private String filePath;
	private String applyState;
	private String appalBz;
	private Date applyDate;
	private String approvalState;
	private String approvalBz;
	private String approvalFileName;
	private String approvalFilePath;
	private Date approvalDate;
	private String monitorState;
	private String monitorBz;
	private String monitorFileName;
	private String monitorFilePath;
	private Date monitorDate;
	private String endState;
	private String endBz;
	private String endFileName;
	private String endFilePath;
	private Date endDate;
	private String teacherId;
	private String teacherName;
	private String state;
	private ProjectService projectService;
	private HttpServletRequest request;
	private HttpSession session;
	private PageView<Project> pageView;
	public int currentpage;
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getProjectId() {
		return projectId;
	}

	public void setProjectId(String projectId) {
		this.projectId = projectId;
	}

	public String getProjectType() {
		return projectType;
	}

	public void setProjectType(String projectType) {
		this.projectType = projectType;
	}

	public String getProjectName() {
		return projectName;
	}

	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}

	public String getProjectOrgin() {
		return projectOrgin;
	}

	public void setProjectOrgin(String projectOrgin) {
		this.projectOrgin = projectOrgin;
	}

	public String getProjectXz() {
		return projectXz;
	}

	public void setProjectXz(String projectXz) {
		this.projectXz = projectXz;
	}

	public Date getStratDate() {
		return stratDate;
	}

	public void setStratDate(Date stratDate) {
		this.stratDate = stratDate;
	}

	public Date getPlanEndDate() {
		return planEndDate;
	}

	public void setPlanEndDate(Date planEndDate) {
		this.planEndDate = planEndDate;
	}

	public String getCyrs() {
		return cyrs;
	}

	public void setCyrs(String cyrs) {
		this.cyrs = cyrs;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public String getProjectFile() {
		return projectFile;
	}

	public void setProjectFile(String projectFile) {
		this.projectFile = projectFile;
	}

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public String getApplyState() {
		return applyState;
	}

	public void setApplyState(String applyState) {
		this.applyState = applyState;
	}

	public String getAppalBz() {
		return appalBz;
	}

	public void setAppalBz(String appalBz) {
		this.appalBz = appalBz;
	}

	public Date getApplyDate() {
		return applyDate;
	}

	public void setApplyDate(Date applyDate) {
		this.applyDate = applyDate;
	}

	public String getApprovalState() {
		return approvalState;
	}

	public void setApprovalState(String approvalState) {
		this.approvalState = approvalState;
	}

	public String getApprovalBz() {
		return approvalBz;
	}

	public void setApprovalBz(String approvalBz) {
		this.approvalBz = approvalBz;
	}

	public String getApprovalFileName() {
		return approvalFileName;
	}

	public void setApprovalFileName(String approvalFileName) {
		this.approvalFileName = approvalFileName;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getApprovalFilePath() {
		return approvalFilePath;
	}

	public void setApprovalFilePath(String approvalFilePath) {
		this.approvalFilePath = approvalFilePath;
	}

	public Date getApprovalDate() {
		return approvalDate;
	}

	public void setApprovalDate(Date approvalDate) {
		this.approvalDate = approvalDate;
	}

	public String getMonitorState() {
		return monitorState;
	}

	public void setMonitorState(String monitorState) {
		this.monitorState = monitorState;
	}

	public String getMonitorBz() {
		return monitorBz;
	}

	public void setMonitorBz(String monitorBz) {
		this.monitorBz = monitorBz;
	}

	public String getMonitorFileName() {
		return monitorFileName;
	}

	public void setMonitorFileName(String monitorFileName) {
		this.monitorFileName = monitorFileName;
	}

	public String getMonitorFilePath() {
		return monitorFilePath;
	}

	public void setMonitorFilePath(String monitorFilePath) {
		this.monitorFilePath = monitorFilePath;
	}

	public Date getMonitorDate() {
		return monitorDate;
	}

	public void setMonitorDate(Date monitorDate) {
		this.monitorDate = monitorDate;
	}

	public String getEndState() {
		return endState;
	}

	public void setEndState(String endState) {
		this.endState = endState;
	}

	public String getEndBz() {
		return endBz;
	}

	public void setEndBz(String endBz) {
		this.endBz = endBz;
	}

	public String getEndFileName() {
		return endFileName;
	}

	public void setEndFileName(String endFileName) {
		this.endFileName = endFileName;
	}

	public String getEndFilePath() {
		return endFilePath;
	}

	public void setEndFilePath(String endFilePath) {
		this.endFilePath = endFilePath;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public String getTeacherId() {
		return teacherId;
	}

	public void setTeacherId(String teacherId) {
		this.teacherId = teacherId;
	}

	public String getTeacherName() {
		return teacherName;
	}

	public void setTeacherName(String teacherName) {
		this.teacherName = teacherName;
	}

	public ProjectService getProjectService() {
		return projectService;
	}

	public void setProjectService(ProjectService projectService) {
		this.projectService = projectService;
	}

	public PageView<Project> getPageView() {
		return pageView;
	}

	public void setCurrentpage(int currentpage) {
		this.currentpage = currentpage;
	}

	/**
	 * 项目发布Action
	 * 
	 * @return
	 */
	public String addProject() throws Exception {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		session.getServletContext();
		Project project = new Project();
		project.setProjectName(getProjectName());
		project.setProjectType(getProjectType());
		project.setProjectOrgin(getProjectOrgin());
		project.setProjectXz(getProjectXz());
		project.setTeacherName(getTeacherName());
		project.setTeacherId((String)session.getAttribute("teacherId"));
		project.setStratDate(getStratDate());
		project.setPlanEndDate(getPlanEndDate());

		// 初始化状态
		project.setApplyState("未申请");
		project.setApprovalState("未立项");
		project.setMonitorState("未监控");
		project.setEndState("未结题");

		// project.setFilePath(getFilePath());
		project.setComment(getComment());
		project.setCyrs(getCyrs());
		Boolean flag = projectService.addProject(project);
		if (flag) {
			request.setAttribute("project",project);
			request.setAttribute("message", "项目发布成功");
			return "success";
		} else {
			request.setAttribute("message", "项目发布失败,请重试！");
			return "failed";
		}
	}

	/**
	 * 更新项目信息
	 * 
	 * @return
	 */
	public String updateProject() throws Exception {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		session.getServletContext();
		int projectId = Integer.parseInt(request.getAttribute("projectId")
				.toString());
		Project project = projectService.findProjectById(projectId);
		project.setProjectName(getProjectName());
		project.setProjectType(getProjectType());
		project.setProjectOrgin(getProjectOrgin());
		project.setTeacherName(getTeacherName());
		project.setProjectXz(getProjectXz());
		project.setStratDate(getStratDate());
		project.setPlanEndDate(getPlanEndDate());
		// project.setFilePath(getFilePath());
		project.setComment(getComment());
		project.setCyrs(getCyrs());
		Boolean flag = projectService.updateProject(project);
		if (flag) {
			List<Project> projectlist = projectService.findAllProject();
			request.setAttribute("projectlist", projectlist);
			request.setAttribute("message", "更新成功！");
			return "success";
		} else {
			request.setAttribute("message", "更新失败,请重试！");
			return "failed";
		}

	}

	/**
	 * 删除项目
	 * 
	 * @return
	 */
	public String deleteProject() throws Exception {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		session.getServletContext();
		int projectId = Integer.parseInt(request.getAttribute("projectId")
				.toString());
		Boolean flag = projectService.deleteProjectByProjectId(projectId);
		if (flag) {
			List<Project> projectlist = projectService.findAllProject();
			request.setAttribute("projectlist", projectlist);
			request.setAttribute("message", "项目信息删除成功！");
			return "success";
		} else {
			request.setAttribute("message", "项目信息删除失败,请重试！");
			return "failed";
		}

	}

	/**
	 * 查看项目详细信息
	 * 
	 * @return
	 */
	public String projectInfo() throws Exception {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		session.getServletContext();
		int projectId = Integer.parseInt(request.getAttribute("projectId")
				.toString());
		Project project = projectService.findProjectById(projectId);
		request.setAttribute("project", project);
		return "success";
	}

	/**
	 * 项目列表
	 * 
	 * @return
	 */
	public String listProject() throws Exception {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		session.getServletContext();
		List<Project> projectlist = projectService.findAllProject();
		request.setAttribute("projectlist", projectlist);
		return "success";

	}

	/**
	 * 项目申请列表
	 * 
	 * @return
	 */
	public String listProApply() throws Exception {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		session.getServletContext();
		/*
		 * String projectId = request.getAttribute("projectId").toString();
		 * Project project =projectService.findProjectById(projectId);
		 * Map<ProApply,Teacher> map = projectService.findProApply(projectId);
		 * request.setAttribute("project",project); request.setAttribute("map",
		 * map);
		 */
		List<Project> projectlist = projectService.findAllProject();
		request.setAttribute("projectlist", projectlist);
		return "success";
	}

	/**
	 * 项目立项列表
	 * 
	 * @return
	 */
	public String listProApproval() throws Exception {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		session.getServletContext();
		/*
		 * String projectId = request.getAttribute("projectId").toString();
		 * Project project =projectService.findProjectById(projectId);
		 * Map<ProApproval,Teacher> map =
		 * projectService.findProApproval(projectId);
		 * request.setAttribute("project",project); request.setAttribute("map",
		 * map);
		 */
		List<Project> projectlist = projectService.findAllProject();
		request.setAttribute("projectlist", projectlist);
		return "success";
	}

	/**
	 * 项目执行审批列表
	 * 
	 * @return
	 */
	public String listProMonitor() throws Exception {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		session.getServletContext();
		/*
		 * String projectId = request.getAttribute("projectId").toString();
		 * Project project =projectService.findProjectById(projectId);
		 * Map<ProMonitor,Teacher> map =
		 * projectService.findProMonitor(projectId);
		 * request.setAttribute("project",project); request.setAttribute("map",
		 * map);
		 */
		List<Project> projectlist = projectService.findAllProject();
		request.setAttribute("projectlist", projectlist);
		return "success";
	}

	/**
	 * 项目结题审批列表
	 * 
	 * @return
	 */
	public String listProEnd() throws Exception {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		session.getServletContext();
		/*
		 * String projectId = request.getAttribute("projectId").toString();
		 * Project project =projectService.findProjectById(projectId);
		 * Map<ProEnd,Teacher> map = projectService.findProEnd(projectId);
		 * request.setAttribute("project",project); request.setAttribute("map",
		 * map);
		 */
		List<Project> projectlist = projectService.findAllProject();
		request.setAttribute("projectlist", projectlist);
		return "success";
	}

	// 项目流程

	// 项目提交申请
	public String ApplyState() throws Exception {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		int projectId = Integer.parseInt(request.getAttribute("projectId")
				.toString());
		Project project = projectService.findProjectById(projectId);
		project.setApplyState("申请中");
		projectService.updateProject(project);
		List<Project> projectlist = projectService.findAllProject();
		request.setAttribute("projectlist", projectlist);
		request.setAttribute("已成功提交申請","message");
		return SUCCESS;

	}

	// 项目立项
	public String ApprovalState() throws Exception {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		int projectId = Integer.parseInt(request.getAttribute("projectId")
				.toString());
		Project project = projectService.findProjectById(projectId);
		project.setApprovalState("立项中");
		projectService.updateProject(project);
		List<Project> projectlist = projectService.findAllProject();
		request.setAttribute("projectlist", projectlist);
		request.setAttribute("已成功提交立项","message");
		return SUCCESS;

	}

	// 项目监控
	public String MonitorState() throws Exception {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		int projectId = Integer.parseInt(request.getAttribute("projectId")
				.toString());
		Project project = projectService.findProjectById(projectId);
		project.setMonitorState("监控中");
		projectService.updateProject(project);
		List<Project> projectlist = projectService.findAllProject();
		request.setAttribute("projectlist", projectlist);
		request.setAttribute("已成功提交监控","message");
		return SUCCESS;

	}

	// 项目结题
	public String EndState() throws Exception {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		int projectId = Integer.parseInt(request.getAttribute("projectId")
				.toString());
		Project project = projectService.findProjectById(projectId);
		project.setEndState("已提交结题");
		projectService.updateProject(project);
		List<Project> projectlist = projectService.findAllProject();
		request.setAttribute("projectlist", projectlist);
		request.setAttribute("已成功提交结题","message");
		return SUCCESS;

	}

	// 管理員項目審批流程

	// 项目提交申请
	public String adminApplyState() throws Exception {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		int projectId = Integer.parseInt(request.getAttribute("projectId")
				.toString());
		Project project = projectService.findProjectById(projectId);

		if ("0".equals(getState())) {
			project.setApplyState("申请不通过");
		} else {
			project.setApplyState("申请通过");
		}

		projectService.updateProject(project);
		List<Project> projectlist = projectService.findAllProject();
		request.setAttribute("projectlist", projectlist);
		return SUCCESS;

	}

	// 项目立项
	public String adminApprovalState() throws Exception {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		int projectId = Integer.parseInt(request.getAttribute("projectId")
				.toString());
		Project project = projectService.findProjectById(projectId);

		if ("0".equals(getState())) {
			project.setApprovalState("立项不通过");
		} else {
			project.setApprovalState("立项通过");
		}

		projectService.updateProject(project);
		List<Project> projectlist = projectService.findAllProject();
		request.setAttribute("projectlist", projectlist);
		return SUCCESS;

	}

	// 项目监控
	public String adminMonitorState() throws Exception {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		int projectId = Integer.parseInt(request.getAttribute("projectId")
				.toString());
		Project project = projectService.findProjectById(projectId);

		if ("0".equals(getState())) {
			project.setMonitorState("监控不通过");
		} else {
			project.setMonitorState("监控通过");
		}

		projectService.updateProject(project);
		List<Project> projectlist = projectService.findAllProject();
		request.setAttribute("projectlist", projectlist);
		return SUCCESS;

	}

	// 项目结题
	public String adminEndState() throws Exception {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		int projectId = Integer.parseInt(request.getAttribute("projectId")
				.toString());
		Project project = projectService.findProjectById(projectId);

		if ("0".equals(getState())) {
			project.setEndState("结题不通过");
		} else {
			project.setEndState("结题通过");
		}
		
		projectService.updateProject(project);
		List<Project> projectlist = projectService.findAllProject();
		request.setAttribute("projectlist", projectlist);
		return SUCCESS;

	}
	
	/**
	 * 根据teacherId来查找相关的项目列表,客户端的项目列表呈现
	 */
	public String findProjectByTeacherId() throws Exception{
		request = ServletActionContext.getRequest();
		session = request.getSession();
		String teacherId = (String) session.getAttribute("teacherId");
		pageView=new PageView<Project>();
		if(teacherId!=null){
			QueryResult<Project> queryResult = projectService.findByTeacherId(teacherId,pageView.getFirstResult(),pageView.getMaxresult());
			pageView.setQueryResult(queryResult);
			request.setAttribute("pageView", pageView);
			request.setAttribute("projectlist", queryResult.getResultlist());
			return "success";
		}
		return "error";
		
	}


}
